import request from '@/utils/request/index'
import type { RoleResponseData, RoleData, MenuResponseData } from './type'
enum API {
  ALLROLE_URL = '/admin/acl/role',
  ADDROLE_URL = '/admin/acl/role/save',
  UPDATEROLE_URL = '/admin/acl/role/update',
  ALLPERMISSION_URL = '/admin/acl/permission/toAssign',
  SETPERMISSION_URL = '/admin/acl/permission/doAssign',
  REMOVEROLE_URL = '/admin/acl/role/remove'
}

export const reqAllRoleList = (page: number, limit: number, roleName: string) =>
  request.get<RoleResponseData>(
    `${API.ALLROLE_URL}/${page}/${limit}?roleName=${roleName}`
  )

export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request.put<null>(`${API.UPDATEROLE_URL}`, data)
  } else {
    return request.post<null>(`${API.ADDROLE_URL}`, data)
  }
}

export const reqAllMenuList = (roleId: number) =>
  request.get<MenuResponseData>(`${API.ALLPERMISSION_URL}/${roleId}`)

export const reqSetPermisson = (roleId: number, permissionId: number[]) =>
  request.post<null>(
    `${API.SETPERMISSION_URL}?roleId=${roleId}&permissionId=${permissionId}`
  )
export const reqRemoveRole = (roleId: number) =>
  request.delete<null>(`${API.REMOVEROLE_URL}/${roleId}`)
